package com.ruoyi.framework.xa;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
@Service
public class XATransferServiceServiceImpl implements TransferService{
    @Autowired
    @Qualifier("mJdbcTemplate")
    private JdbcTemplate mJdbcTemplate;
 
    @Autowired
    @Qualifier("sJdbcTemplate")
    private JdbcTemplate sJdbcTemplate;
 
    @Transactional
    public String transfer(int money) {
        int resultNan = mJdbcTemplate.update("INSERT INTO bank_b(money,user_name)VALUES (?,?)",-money,"james");
        int resultDao = sJdbcTemplate.update("INSERT INTO bank_b(money,user_name)VALUES (?,?)",money,"peter");
        if (money > 200){
            throw new RuntimeException("money too large");//大于200后就回滚，以上两条sql操作无效
        }
        return resultNan+"-"+resultDao;
    }
 
}